package com.dgut.web;

import com.dgut.model.PageBean;
import com.dgut.model.UserInfo;
import com.dgut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author GooRay
 * 创建于 2020/6/23
 */
@Controller
public class SearchController {

    @Autowired
    UserService userService;

    @RequestMapping("/SearchAll")
    public String SearchAll(HttpServletRequest request, HttpServletResponse response, Model model) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if(currentPage==null||"".equals(currentPage)){
            currentPage = "1";
        }
        if(rows==null||"".equals(rows)){
            rows = "5";
        }
        Map<String, String[]> condition = request.getParameterMap();
        Map<String, String> map = new HashMap<>();
        Set<String> keys = condition.keySet();
        for (String key : keys) {
            map.put(key,condition.get(key)[0]);
        }
        PageBean<UserInfo> pageBean = userService.findUserByPage(currentPage,rows,condition);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("condition", map);
        return "UserInfo";
    }

    @RequestMapping("/Warning")
    public String Warning(HttpServletRequest request, HttpServletResponse response, Model model) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if(currentPage==null||"".equals(currentPage)){
            currentPage = "1";
        }
        if(rows==null||"".equals(rows)){
            rows = "5";
        }
        Map<String, String[]> condition = new HashMap<>();
        String[] temp = {"37.3"};
        condition.put("temp",temp);
        PageBean<UserInfo> pageBean = userService.findUserByPage(currentPage,rows,condition);
        model.addAttribute("pageBean",pageBean);
        return "Warning";
    }


}
